//
//  GroupUserListVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/10/12.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "GroupUserListVC.h"
#import "FriendCell.h"
#import "YBApiManager+Chat.h"
#import "FriendInfoListVC.h"

@interface GroupUserListVC ()
@property (nonatomic, strong) NSMutableArray *selectedFriendIdsArryay;

@end

@implementation GroupUserListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"全部群成员";
    [self setupUI];
    [self requestDatas];
}
- (void)setupUI {
    [self.tableView customRegisterClassWithName:@"FriendCell"];
    if (self.listType == 1) {
        self.rightTitle = @"删除";
        self.rightButton.enabled = NO;
        self.leftTitle = @"取消";
    }
}
- (void)requestDatas {
    [MBProgressHUD showActivityMessageInView:@""];
    
    [[YBApiManager.shareManager getGroupAllUserWithGroupId:self.group.group_id] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        NSArray *dataArr = [x objectForKey:@"data"];
        
        self.dataSource = [FriendItem mj_objectArrayWithKeyValuesArray:dataArr];
        [self.tableView reloadData];
        
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendCell" forIndexPath:indexPath];
    
    FriendItem *friend = self.dataSource[indexPath.row];
    cell.friendItem = friend;
    if (self.listType == 1) {
        if (friend.isMe == YES) {
            cell.selectBtn.enabled = NO;
        }else {
            cell.selectBtn.selected = [self.selectedFriendIdsArryay containsObject:friend.friend_id];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendItem *user = self.dataSource[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.listType == 1) {
        if (user.isMe == YES) {
            return;
        }
        FriendCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.selectBtn.selected = !cell.selectBtn.selected;
        if ([self.selectedFriendIdsArryay containsObject:user.friend_id] == YES) {
            cell.selectBtn.selected = NO;
            [self.selectedFriendIdsArryay removeObject:user.friend_id];
        } else {
            cell.selectBtn.selected = YES;
            [self.selectedFriendIdsArryay addObject:user.friend_id];
        }
        if (self.selectedFriendIdsArryay.count > 0) {
            self.rightTitle = [NSString stringWithFormat:@"删除(%ld)", self.selectedFriendIdsArryay.count];
            self.rightButton.enabled = YES;
        } else {
            self.rightTitle = @"删除";
            self.rightButton.enabled = NO;
        }
        
        return;
    }
    
    FriendInfoListVC *userDetailVC = [FriendInfoListVC new];
    userDetailVC.from = @"0";
    userDetailVC.listType = 2;
    userDetailVC.user = self.dataSource[indexPath.row];
    
    [self.navigationController pushViewController:userDetailVC animated:YES];
    
    
}


- (void)handleRightItemAction {
    if (self.selectedFriendIdsArryay.count <= 0) {
        [MBProgressHUD showTipMessageInView:@"请选择联系人" timer:1.0f];
        return;
    }
    [LBXAlertAction showAlertWithTitle:@"确定删除?" msg:nil buttonsStatement:@[@"取消", @"确认"] chooseBlock:^(NSInteger buttonIdx) {
        if (buttonIdx == 1 && self.didClickDeleteGroupUserBlock) {
            self.didClickDeleteGroupUserBlock(self.selectedFriendIdsArryay);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}
- (NSMutableArray *)selectedFriendIdsArryay {
    if (_selectedFriendIdsArryay == nil) {
        _selectedFriendIdsArryay = [NSMutableArray array];
    }
    return _selectedFriendIdsArryay;
}

@end
